package com.apobates.jforum2.troll.forum.entity;

import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

/**
 * 轮播图的幻灯片
 *
 * @author xiaofanku
 * @since 20200614
 */
@Entity
@Table(name = "apo_topic_carousel_slide", uniqueConstraints = {@UniqueConstraint(columnNames = {"link", "imageAddr"})})
public class CarouselSlide implements Serializable, Comparable<CarouselSlide>{
    private static final long serialVersionUID = 4230786860004000283L;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private int id;
    //标题
    private String title;
    //跳转后的地址
    private String link;
    //图片的[编码|解码]地址
    private String imageAddr;
    //显示顺序
    private int ranking;
    //所属的轮播图
    private int topicCarouselId;
    //true可用,false禁用
    private boolean status;
    
    //empty constructor for JPA instantiation
    public CarouselSlide() {
        super();
    }
    
    public CarouselSlide(int topicCarouselId, String title, String link, String imageAddr, int ranking, boolean status) {
        this.id = 0;
        this.title = title;
        this.link = link;
        this.imageAddr = imageAddr;
        this.ranking = ranking;
        this.status = status;
        this.topicCarouselId = topicCarouselId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getImageAddr() {
        return imageAddr;
    }

    public void setImageAddr(String imageAddr) {
        this.imageAddr = imageAddr;
    }

    public int getRanking() {
        return ranking;
    }

    public void setRanking(int ranking) {
        this.ranking = ranking;
    }

    public int getTopicCarouselId() {
        return topicCarouselId;
    }

    public void setTopicCarouselId(int topicCarouselId) {
        this.topicCarouselId = topicCarouselId;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }
    
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 43 * hash + Objects.hashCode(this.link);
        hash = 43 * hash + Objects.hashCode(this.imageAddr);
        return hash;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CarouselSlide other = (CarouselSlide) obj;
        if (!Objects.equals(this.link, other.link)) {
            return false;
        }
        if (!Objects.equals(this.imageAddr, other.imageAddr)) {
            return false;
        }
        return true;
    }
    
    @Override
    public int compareTo(CarouselSlide o) {
        return ranking - o.getRanking();
    }
    
    @Transient
    public static CarouselSlide empty(int topicCarouselId) {
        return new CarouselSlide(topicCarouselId, "", "", null, 1, true);
    }
}